﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebPortal.Models.EntityModels;
using WebPortal.Models.ViewModel;
using WebPortal.Helpers;
using System.Drawing.Imaging;
using System.Web.Script.Serialization;

namespace WebPortal.Controllers
{
    public class AuthController : BaseController
    {
        //
        // GET: /Auth/

        public string Login(Users u)
        {
            Dictionary<string, int> response = new Dictionary<string, int>();
            int result = LoginByEmail(u);
            response.Add("success", result>0 ? 1 : 0);
            response.Add("userID", result);
            return new JavaScriptSerializer().Serialize(response);
        }

        public string Register(UserRegistrationModel u)
        {
            Dictionary<string, int> response = new Dictionary<string, int>();
            response.Add("success", 0);
            bool correctCaptcha = Session["CaptchaImageText"]!=null && u.Captcha.Equals(Session["CaptchaImageText"].ToString());
            if (ModelState.IsValid && correctCaptcha)
            {
                Users user = new Users();
                user.IsAdmin = false;
                user.Email = u.Email;
                user.Password = u.Password;
                dbUsers.AddToUsers(user);
                try
                {
                    dbUsers.SaveChanges();
                }
                catch (System.Data.UpdateException e)
                {
                    //Can not insert to database
                    response.Add("messageCode", 0);
                    return new JavaScriptSerializer().Serialize(response);
                }
                return Login(user);
            }
            else
            {
                //Wrong validation
                response.Add("messageCode", 1);
                return new JavaScriptSerializer().Serialize(response);
            }
        }

        public ActionResult Logout()
        {
            HttpCookie userID = new HttpCookie("userID");
            userID.Expires = DateTime.Now.AddDays(-1d);
            HttpCookie pass = new HttpCookie("pass");
            pass.Expires = DateTime.Now.AddDays(-1d);
            Response.Cookies.Set(userID);
            Response.Cookies.Set(pass);
            return RedirectToAction("Index", "Main");
        }

        public void Captcha()
        {
            this.Session["CaptchaImageText"] = CaptchaImage.GenerateRandomCode();
            CaptchaImage ci = new CaptchaImage(this.Session["CaptchaImageText"].ToString(), 160, 40, "Arial");

            // Change the response headers to output a JPEG image.
            this.Response.Clear();
            this.Response.ContentType = "image/jpeg";

            // Write the image to the response stream in JPEG format.
            ci.Image.Save(this.Response.OutputStream, ImageFormat.Jpeg);

            // Dispose of the CAPTCHA image object.
            ci.Dispose();
        }

    }
}
